package ltd.hxya.novel.crawl.controller;


import ltd.hxya.novel.common.bean.Result;
import ltd.hxya.novel.crawl.bean.CrawlVo;
import ltd.hxya.novel.crawl.entity.CrawlSource;
import ltd.hxya.novel.crawl.service.ICrawlSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 爬虫源表 前端控制器
 * </p>
 *
 * @author hxya
 * @since 2022-09-18
 */
@RestController
@RequestMapping("/crawl/admin")
public class CrawlSourceController {

    @Autowired
    private ICrawlSourceService crawlSourceService;

    //获取所有的爬虫源
    @GetMapping("/getCrawlSource")
    public Result<List<CrawlSource>> getCrawlSource(){
        List<CrawlSource> crawlSources = crawlSourceService.getBaseMapper().selectList(null);
        return Result.success(crawlSources);
    }

    //根据爬虫id获取爬虫源和爬虫规则
    @GetMapping("/getCrawlAndRule/{crawlId}")
    public Result<CrawlVo> getCrawlVo(@PathVariable Integer crawlId){
        CrawlVo crawlVo = crawlSourceService.getCrawlSourceById(crawlId);
        return Result.success(crawlVo);
    }

    //获取默认的爬虫源信息
    @GetMapping("/defaultCrawlAndRule")
    public Result<CrawlVo> defaultCrawlVo() {
        CrawlVo crawlVo = crawlSourceService.defaultCrawlSource();
        return Result.success(crawlVo);
    }

    //修改爬虫源状态
    @PostMapping("/changeCrawlSourceStatus")
    public Result changeCrawlSourceStatus(@RequestBody CrawlSource crawlSource){
        crawlSourceService.changeCrawlStatus(crawlSource);
        return Result.success();
    }

    //添加爬虫源
    @PutMapping("/addCrawlSource")
    public Result addCrawlSource(@RequestBody CrawlSource crawlSource){
        crawlSourceService.addCrawlSource(crawlSource);
        return Result.success();
    }

    /**
     * 删除爬虫源信息
     */
    @DeleteMapping("/deleteCrawlSource")
    public Result deleteCrawlSource(Integer[] ids){
        crawlSourceService.deleteCrawlSource(ids);
        return Result.success();
    }

    /**
     * 修改爬虫源信息
     */
    @PostMapping("/updateCrawlSource")
    public Result updateCrawlSource(@RequestBody CrawlSource crawlSource){
        crawlSourceService.updateCrawlSource(crawlSource);
        return Result.success();
    }

    /**
     * 根据小说id，查询爬虫源信息
     */
    @GetMapping("/crawlSourceByBookId")
    public Result<CrawlVo> crawlSourceByBookId(String bookId){
        CrawlVo crawlVo = crawlSourceService.crawlSourceByBookId(bookId);
        return Result.success(crawlVo);
    }

}
